﻿Imports System.ServiceModel.Discovery
Imports System.ServiceModel
Imports System.ServiceModel.Description
Imports System.Collections.ObjectModel

Public Class Client2ClientHelper
    Public Shared Property DiscoveryClientInstance As DiscoveryClient

    Public Shared Property FoundClients As New ObservableCollection(Of DiscoveredClientItem)


    Public Shared Sub AsyncDiscoverOtherClients()
        FoundClients.Clear()
        AddHandler DiscoveryClientInstance.FindProgressChanged, AddressOf NewClientDiscovered
        AddHandler DiscoveryClientInstance.FindCompleted, AddressOf SearchCompleted

        DiscoveryClientInstance.FindAsync(New FindCriteria(GetType(ClientMessagesService)))
    End Sub

    Public Shared Sub CreateAndStartClientMessageService()

        Dim random = New Random
        Dim po = 27200
        Dim done As Boolean = False
        While (Not done)
            Try
                Dim ur = New Uri("net.tcp://localhost:" & (po + 1).ToString & "/")
                My.Application.ClientMessageServiceHost = New ServiceHost(GetType(ClientMessagesService), ur)
                Dim host = My.Application.ClientMessageServiceHost
                Dim b As New NetTcpBinding
                b.MaxReceivedMessageSize = 1024 * 1024 * 10
                b.SendTimeout = TimeSpan.FromSeconds(150)
                b.ReceiveTimeout = TimeSpan.FromSeconds(150)
                b.ReaderQuotas.MaxArrayLength = 1024 * 1024 * 10
                host.AddServiceEndpoint(GetType(IClientMessagesService), b, "ClientMessageService")
                host.Description.Behaviors.Add(New ServiceDiscoveryBehavior())

                Dim smb As New ServiceMetadataBehavior
                smb.HttpGetEnabled = True
                'set to 
                smb.HttpGetUrl = New Uri("http://localhost:" & po.ToString & "/ClientMessageService/metadata")
                host.Description.Behaviors.Add(smb)

                host.AddServiceEndpoint(New UdpDiscoveryEndpoint())

                host.Open()

                DiscoveryClientInstance = New DiscoveryClient(New UdpDiscoveryEndpoint())
                done = True
            Catch ex As Exception
                po = po + 2
                Continue While
            End Try

        End While
    End Sub

    Private Shared Sub NewClientDiscovered(sender As Object, e As FindProgressChangedEventArgs)
        Dim endpoint = e.EndpointDiscoveryMetadata

        If endpoint.Address.Uri.Authority <> My.Application.ClientMessageServiceHost.BaseAddresses(0).Authority Then
            Dim cms As New ClientServices.ClientMessagesServiceClient
            cms.Endpoint.Address = endpoint.Address

            FoundClients.Add(New DiscoveredClientItem(cms))
        End If
    End Sub

    Private Shared Sub SearchCompleted(sender As Object, e As FindCompletedEventArgs)
        RemoveHandler DiscoveryClientInstance.FindProgressChanged, AddressOf NewClientDiscovered
        RemoveHandler DiscoveryClientInstance.FindCompleted, AddressOf SearchCompleted
        RaiseEvent SearchCompletedEvent()
    End Sub
    Public Shared Event SearchCompletedEvent()


End Class
